package com.cakebackoffice;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.cakebackoffice.Model.PaymentType;

import java.util.List;


public class AdminParamActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adminparamactivity);
        List<PaymentType> paymentType = PaymentType.getAll(AdminParamActivity.this);
        for (PaymentType p : paymentType) {
            bookingFeeValue = p.getBookingFee();
        }
    }

    static final int CHANGE_INVOICING_DIALOG = 0;
    static final int CHANGE_NAME_DIALOG = 1;
    static final int CHANGE_ADRESS_DIALOG = 2;
    private Dialog dialog;
    private Float bookingFeeValue = 0f;


    public void openInvoicingDialog(View view) {
        showDialog(CHANGE_INVOICING_DIALOG);
    }


    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case CHANGE_INVOICING_DIALOG:
                dialog = new Dialog(AdminParamActivity.this);
                dialog.setContentView(R.layout.invoicingdialog);

                dialog.setTitle("Invoicing Charge");

                ((EditText) (dialog.findViewById(R.id.numberPicker))).setText(bookingFeeValue.toString());

                ((Button) (dialog.findViewById(R.id.SaveButton))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            bookingFeeValue = new Float(((EditText) (dialog.findViewById(R.id.numberPicker))).getText().toString());
                        } catch (Exception e) {

                        }
                        AdminParamActivity.this.saveInvoicing(view);
                    }
                });
                ((Button) (dialog.findViewById(R.id.CancelButton))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdminParamActivity.this.cancelChanges(view);
                    }
                });


                break;
            case CHANGE_NAME_DIALOG:


                break;

            default:
                dialog = null;
        }
        return dialog;
    }

    private void cancelChanges(View view) {
        this.dialog.dismiss();
    }

    private void saveInvoicing(View view) {
        List<PaymentType> paymentType = PaymentType.getAll(AdminParamActivity.this);
        for (PaymentType p : paymentType) {
            p.setBookingFee(this.bookingFeeValue);
            p.saveToDatabase(this);

        }
        this.dialog.dismiss();
    }
}